(function ($) {
	$.periodInfoSet = function () {
		return {
			init: function(){
				this.listener();
				this.periodInfoSetList(0,0,0);
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
				//点击修改科目等
				$("#periodInfoSet ul").on("click","a",function(){
					$(this).parents("ul").find(".blued").removeClass("blued");
					$(this).addClass("blued");
                    var periodIndex=$(".period .blued").parent().index();
                    var subjectIndex=$(".subject .blued").parent().index();
                    var editionIndex=$(".edition .blued").parent().index();
                    $.periodInfoSet.periodInfoSetList(periodIndex,subjectIndex,editionIndex);
				});
			//	点击保存
				$(".save").on("click",function(){
					var periodId=$(".period .blued").parent().attr("data-periodId");
					var subjectId=$(".subject .blued").parent().attr("data-subjectId");
					var editionId=$(".edition .blued").parent().attr("data-editionId");
					if((periodId==undefined)||(subjectId==undefined)||(editionId==undefined)){
						$(".errorInfo").css("visibility","visible")
					}else{
						var periodInfoSave={
                            token:User.token,
                            periodId:periodId,
                            subjectId:subjectId,
                            editionId:editionId
						};
                        updateObject(Doman.url + Doman.xnPeriodInfoSave, false, JSON.stringify(periodInfoSave), function (result) {
                            if(result.code=="200"){
                                parent.layer.msg(result.message, {icon: 1});
                                setTimeout(function(){
                                    parent.layer.closeAll();
                                },1000);
                            }else{
                                layer.msg(result.message, {icon: 2});
                            }
                        }, function () {
                        });
					}
				});
			},

            periodInfoSetList: function(periodIndex,subjectIndex,editionIndex){
                updateObject(Doman.url + Doman.periodGetAll, false, JSON.stringify({}), function (result) {
                    if (result.code == "200") {
                        if(result.periodDTOList.length>0){
                        	var periodHtml='';
                        	$.each(result.periodDTOList,function(i,periodItem){
                        		if(i==periodIndex){
                                    periodHtml+='<li data-periodId='+periodItem.periodId+'><a class="button blued">'+periodItem.periodName+'</a></li>';
                                    if(periodItem.subjectDTOList.length>0){
                                    	var subjectHtml='';
                                        $.each(periodItem.subjectDTOList,function(j,subjectItem){
                                            if(j==subjectIndex){
                                                subjectHtml+='<li data-subjectId='+subjectItem.subjectId+'><a class="button blued">'+subjectItem.subjectName+'</a></li>';
                                                if(subjectItem.editionDTOList.length>0){
                                                    var editionHtml='';
                                                    $.each(subjectItem.editionDTOList,function(k,editionItem){
                                                        if(k==editionIndex){
                                                            editionHtml+='<li data-editionId='+editionItem.editionId+'><a class="button blued">'+editionItem.editionName+'</a></li>';
														}else{
                                                            editionHtml+='<li data-editionId='+editionItem.editionId+'><a class="button">'+editionItem.editionName+'</a></li>';
														}
													});
                                                    $(".edition ul").html(editionHtml);
												}
                                            }else{
                                                subjectHtml+='<li data-sunjectId='+subjectItem.subjectId+'><a class="button">'+subjectItem.subjectName+'</a></li>';
											}
                                            $(".subject ul").html(subjectHtml);
										});
									}
								}else{
                                    periodHtml+='<li data-periodId='+periodItem.periodId+'><a class="button">'+periodItem.periodName+'</a></li>';
                                }
							});
							$(".period ul").html(periodHtml);
						}
                    }
                }, function () {
                });
			}
		}
	}();
    $.periodInfoSet.init();
})(jQuery);